import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.settings import Settings


class Test:
    app = Settings
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：进入带有输入框的应用(设置)中，点击设备输入框')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        search_area = device.get_element_by_text('搜索设置项')
        device.click_element(search_area)
        # 搜索框
        device.refresh_layout()
        text_input = device.get_element_by_type('TextInput')
        device.click_element(text_input)
        logging.info('预期结果1：键盘能正常唤醒，键盘可正常点击')
        device.refresh_layout()
        keyboard_win = device.soft_keyboard_area()
        assert keyboard_win, '软键盘未拉起'

        logging.info('步骤2：查看唤醒键盘后是否遮挡搜索框')
        _, textinput_y = device.center_of_element(text_input)
        keyboard_y = keyboard_win['y']
        logging.info('预期结果2：未遮挡搜索框，显示正常')
        assert keyboard_y > textinput_y, '键盘有遮挡搜索框'

        logging.info('步骤3：点击键盘各按钮，切换数字键盘')
        device.click(*self.keyboard.key_q)
        device.click(*self.keyboard.abc_123)
        device.click(*self.keyboard.key_q)
        device.click(*self.keyboard.abc_123)
        logging.info('预期结果3：各按钮可用、可切换数字键盘')
        device.refresh_layout()
        device.assert_text_exist('q1')

        logging.info('步骤4：点击收回按钮')
        device.click(*self.keyboard.close)
        logging.info('预期结果4：键盘成功收起')
        device.assert_soft_keyboard_on(expect=False)
